import { ajax } from "../../public/promiseAjax.js";
import * as obj from "../public.js";
import {cookie}from"../../public/cookieFnNew.js"
const o = obj.default;
o.$('i').onclick = function (){
  location.href = './register.html'
}
function loginF(){
  const uname = o.$('.uname')
  const pw = o.$('.password')
  if(uname.value && pw.value){
    ajax({
      url: 'http://localhost:3000/login',
      method: 'post',
      data: {
        username: uname.value,
        password: pw.value
      }
    }).then((res) => {
      const data = JSON.parse(res)
      const userData =  data.data;
      // const str = `tel=${userData.tel}&password=${userData.password}&username=${userData.username}`;
      let str = `nickName:${userData.nickName},username:${userData.username},tel:${userData.tel},password:${userData.password}`;
      if (data.code === 1) {
        const falg = o.$('.rempwd').checked;
        if(falg){
          cookie.setCookie('petUserD',str,{expires:7});
          // localStorage.setItem('petUserD');
        }else{
          cookie.setCookie('petUserD',str);
        }
        location.href = 'http://10.11.55.120:3000/html/index.html'
      } else {
        alert(data.msg)
      }
    })
  }else{
    console.log('密码错误')
  }
}




export { loginF }